#!/usr/bin/env jruby
# -*- coding: us-ascii -*-
# Elizabeth Wiethoff 2008

require 'java'

dir = File.dirname(__FILE__)
require File.join(dir, '..', 'src', 'lib', 'auxil', 'osdetect')

########################################

$SCREEN_MENU_BAR = $MAC
$KMASK = $MAC ? 'meta ' : 'ctrl '

if $MAC
  java.lang.System.setProperty(
    'com.apple.mrj.application.apple.menu.about.name', 'TableMenuLayout')
  java.lang.System.setProperty(
    'apple.laf.useScreenMenuBar', $SCREEN_MENU_BAR.to_s)
end

########################################

class DinnerMenu < javax.swing.JFrame
  include_class 'java.awt.event.KeyEvent'
  include_class 'javax.swing.JMenu'
  include_class 'javax.swing.JMenuBar'
  include_class 'javax.swing.JMenuItem'
  include_class 'javax.swing.KeyStroke'
  
  def initialize
    super('Table Menu Layout')
    setSize(300, 200)
    setLocation(650, 100)
    
    menu_bar = create_menu_bar(create_file_menu)
    setJMenuBar(menu_bar)
  end
  
  private
  
  class MenuMessage
    include java.awt.event.ActionListener
    def actionPerformed(e)
      puts e.getActionCommand  # 'Open', 'Rename...', etc.
    end
  end
  
  class Exiter
    include java.awt.event.ActionListener
    def actionPerformed(e)
      java.lang.System.exit(0)
    end
  end
  
  def create_file_menu
    f = JMenu.new('File')
    o = JMenuItem.new('Open File')
    g = JMenuItem.new('Open All Files in Group')
    r = JMenuItem.new('Rename File...')
    m = JMenuItem.new('Move File...')
    t = JMenuItem.new('Toss File')
    e = JMenuItem.new('Export Table...')
    
    unless $SCREEN_MENU_BAR
      f.setMnemonic(KeyEvent::VK_F)
      o.setMnemonic(KeyEvent::VK_O)
      g.setMnemonic(KeyEvent::VK_G)
      r.setMnemonic(KeyEvent::VK_R)
      m.setMnemonic(KeyEvent::VK_M)
      t.setMnemonic(KeyEvent::VK_T)
      e.setMnemonic(KeyEvent::VK_E)
    end
    
    o.setAccelerator(KeyStroke.getKeyStroke($KMASK + 'O'))
    g.setAccelerator(KeyStroke.getKeyStroke($KMASK + 'alt O'))
    r.setAccelerator(KeyStroke.getKeyStroke($KMASK + 'R'))
    m.setAccelerator(KeyStroke.getKeyStroke($KMASK + 'M'))
    t.setAccelerator(KeyStroke.getKeyStroke($KMASK + 'T'))
    e.setAccelerator(KeyStroke.getKeyStroke($KMASK + 'E'))
    
    [o, g, r, m, t].each do |item|
      item.addActionListener(MenuMessage.new)
      f.add(item)
    end
    f.addSeparator
    e.addActionListener(MenuMessage.new)
    f.add(e)
    
    unless $MAC
      f.addSeparator
      q = JMenuItem.new('Quit')
      q.setMnemonic(KeyEvent::VK_Q)
      q.setAccelerator(KeyStroke.getKeyStroke($KMASK + 'Q'))
      q.addActionListener(Exiter.new)
      f.add(q)
    end
    
    return f
  end
  
  def create_menu_bar(*menus)
    mb = JMenuBar.new
    menus.each { |menu| mb.add(menu) }
    return mb
  end
end

########################################

if $0 == __FILE__
  f = DinnerMenu.new
  f.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE)
  f.setVisible(true)
end
